package net.bat.service.cb;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import net.bat.pt.BObj;
import net.bat.service.HDDefault;
import net.bat.upload.uploadServlet;
import net.bat.util.Encrypt;
import net.coobird.thumbnailator.Thumbnails;
import net.cs.um.UMService;
import net.sourceforge.pinyin4j.PinyinHelper;
import dao.hb.UUser;

public class CBUUser implements IHandleHook {
	final public static String PN_AVATAR="avatar";
	final public static String FMT_AVATAR = "jpg";
	final public static int SIZE_AVATAR = 50;
	public String getPath(String fname){
		return uploadServlet.getBindPath(fname);
	}
	private String setAvatar(String fname,String uid) throws IOException{
	  int pos2=fname.indexOf("]");
	  uploadServlet.makeDir(uid, uploadServlet.getPath(), false);
	  String op = uid+uploadServlet.PATH_SPLITER+PN_AVATAR+"."+FMT_AVATAR;
	  if( pos2!=-1) {
		  String fn =fname.substring(pos2+1);
	  }
	  File fin = new File(getPath(fname));
	  String path_out = getPath(op);
	  uploadServlet.makeDir(path_out,true);
	  File fout = new File(path_out);
	  Thumbnails.of(fin).size(SIZE_AVATAR, SIZE_AVATAR).outputFormat(FMT_AVATAR).toFile(fout);
	  return op;
	}
	private void pbh(char method, HashMap<String, Object> psMap,
			HttpServletRequest request) throws Exception {
		BObj[] objs = (BObj[])psMap.get("objs");
		for (BObj cur : objs) {
			HashMap<String,Object>  its = cur.getItems();
			String pwd = (String)its.get("pwd");
			if(pwd!=null && pwd.length()<30){
				its.put("pwd", 
						Encrypt.calcUserEncrypt((String)its.get("userId"), 
						(String)its.get("pwd")));
			}
			//对头像进行抽点 180p, 50p
			String avatar=(String)its.get(PN_AVATAR);
			if(avatar != null){
				its.put(PN_AVATAR, setAvatar(avatar,(String)its.get("id")));
			}
		}
	}
	private void gbh(char method, HashMap<String, Object> psMap,
			HttpServletRequest request) throws Exception {
		String hql =(String)psMap.get("hql");
		UUser usr = UMService.getUserBySession(request);
		int uid = usr.getId();
		if(hql.indexOf("where")==-1){
			hql+=" where uid="+uid+" or id="+uid;
		}else{
			hql+=" and (uid="+usr.getId()+" or id="+uid+")";
		}
		psMap.put("hql",hql);		
	}

	@Override
	public void afterHandle(char method, HashMap<String, Object> psMap,
			HttpServletRequest request, Object result) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeHandle(char method, HashMap<String, Object> psMap,
			HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		switch(method){
		case HDDefault.METHOD_PUT:
			pbh(method,psMap,request);
			return;
		default:
			gbh(method,psMap,request);
			break;
		}
	}
	
	public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }
}
